﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CrossSiteCollectionSearchConfigurator
{
    class SearchSettingsPropogatorInstaller : SPFeatureReceiver
    {
        /// <summary>
        /// Load a property from the jobs defintion.
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        private string GetJobProperty(SPFeatureReceiverProperties properties, string propertyName)
        {
            SPFeatureProperty result = properties.Definition.Properties[propertyName];
            if (result == null)
            {
                throw new ApplicationException(string.Format("Feature defintion must contain a {0} property",
                                                                propertyName));
            }
            return result.Value;
        }
        //install the timer job on activation
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            string jobName = GetJobProperty(properties, "jobName");

            SPWebApplication app = properties.Feature.Parent as SPWebApplication;


            foreach (SPJobDefinition job in app.JobDefinitions)
            {
                //Remove the job if it is already registered.
                if (job.Name == jobName)
                {
                    job.Delete();
                }
            }

            //create the job
            SearchSettingsPropogatorJob newJob = new SearchSettingsPropogatorJob(jobName,
                                                                                    app,
                                                                                    SPServer.Local,
                                                                                    SPJobLockType.Job);
            //Set the schedule
            string schedule = GetJobProperty(properties, "jobSchedule");
            newJob.Schedule = SPSchedule.FromString(schedule);
            newJob.Update();


        }

        //Uninstall the timer job on deactivation
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            string jobName = GetJobProperty(properties, "jobName");

            SPWebApplication app = properties.Feature.Parent as SPWebApplication;

            //Remove the job if it is already registered.
            foreach (SPJobDefinition job in app.JobDefinitions)
            {
                if (job.Name == jobName)
                {
                    job.Delete();
                }
            }
        }

        #region Unused overrides
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
        #endregion
    }
}
